import * as Cesium from 'cesium'

/**
 * @create zgf
 */
class MapTools {
    constructor() {
    }

    /**
     * 获取地图的等级
     * @param viewer
     */
    getMapLevel(viewer: any) {
        let height = Math.ceil(viewer.camera.positionCartographic.height);
        let A = 40487.57;
        let B = 0.00007096758;
        let C = 91610.74;
        let D = -40467.74;
        //开根号
        let zoom = D + (A - D) / (1 + Math.pow(height / C, B));
        return zoom.toFixed(3);
    }

    /**
     * 笛卡尔3转经纬度
     * @param viewer
     * @param cartesianPosition
     */
    car3Tojwd(viewer: any, cartesianPosition: any) {
        const ellipsoid = viewer.scene.globe.ellipsoid;
        const cartographicPosition = ellipsoid.cartesianToCartographic(cartesianPosition);
        let x = Cesium.Math.toDegrees(cartographicPosition.longitude);
        let y = Cesium.Math.toDegrees(cartographicPosition.latitude);
        let z = cartographicPosition.height;
        return {x, y, z}
    }

    /**
     * 设置地图的等级
     * @param viewer
     * @param level
     */
    setMapLevel(viewer: any, level: number) {
        var A = 40487.57;
        var B = 0.00007096758;
        var C = 91610.74;
        var D = -40467.74;
        //表示应该缩放到的高度
        let height = C * Math.exp(Math.log(((A - D) / (level - D)) - 1) / B);
        let {x, y, z} = this.car3Tojwd(viewer, viewer.camera.position)
        viewer.camera.flyTo({
            destination: Cesium.Cartesian3.fromDegrees(x, y, height),
            duration: 1.0
        });
        return true;
    }

    /**
     * 高亮实体
     * @param viewer
     * @param target  需要高亮的目标
     * @param options
     * {
     * "iconFont":"需要高亮的图标",
     * fill:"",
     * "opacity":"",
     * "stroken":"",
     * "width":3,
     * "titleColor":"3dtiles的feature的颜色"
     * }
     */
    hightLightEntity(viewer: any, target: any, options: any) {
        let highLightAftertList: any = [];
        let {iconFont, fill, opacity, stroken, width, titleColor} = options;
        if (target.length !== 0) {
            target.forEach((item: any) => {
                if (item?._billboard) { //高亮点
                    if (!iconFont) {
                        item._billboard.scale=1.5;
                    } else {//更改图标高亮
                        item._billboard.setImage(new Date().getTime() + '', iconFont)
                    }
                    highLightAftertList.push(item);
                } else if (item?._polygon) { //高亮面
                    let polygon = item._polygon.clone();
                    polygon.material = Cesium.Color.fromCssColorString(fill).withAlpha(opacity)
                    let entity = viewer.entities.add({
                        id: 'hightLight_polygon' + new Date(),
                        polygon: polygon
                    });
                    highLightAftertList.push(entity);
                } else if (item?._polyline) {//高亮线
                    let polyline = item._polyline.clone();
                    polyline.material= Cesium.Color.fromCssColorString(stroken);
                    // polyline.material = new Cesium.PolylineGlowMaterialProperty({
                    //     glowPower: 1, // 一个数字属性，指定发光强度，占总线宽的百分比。
                    //     color: stroken,
                    // })
                    let entity = viewer.entities.add({
                        name: 'hightLight_polyline' + new Date(),
                        polyline: polyline
                    });
                    highLightAftertList.push(entity);
                } else {//倾斜摄影
                    if (item instanceof Cesium.Cesium3DTileFeature) {
                        item.color = Cesium.Color.fromCssColorString(titleColor)
                    }
                }
            })
            return highLightAftertList;
        } else {
            return false;
        }
    }

    /**
     * 移除高亮
     * @param viewer  移除高亮的目标
     * @param target 几个
     */
    removehightLightEntity(viewer: any, target: any) {
        if (target.length !== 0) {
            target.forEach((item: any) => {
                if (item?._billboard) { //高亮点
                    // item._billboard.setScale(null);
                    item._billboard.scale=0.4;
                } else if (item?._polygon || item?._polyline) { //高亮面
                    viewer.entities.remove(item)
                } else {//倾斜摄影
                    if (item instanceof Cesium.Cesium3DTileFeature) {
                        // @ts-ignore
                        item['color'] = null
                    }
                }
            })
        } else {
            return false;
        }
    }

    /**
     * 设置透明度
     * @param viewer
     * @param opacity 透明度的opacity
     */
    setOpacity(viewer: any, opacity: number) {
        viewer.scene.globe.showGroundAtmosphere = false;
        viewer.scene.globe.baseColor = Cesium.Color.TRANSPARENT;
        viewer.scene.globe.translucency.enabled = true;
        viewer.scene.globe.undergroundColor = undefined;
        viewer.scene.skyBox.show = false;
        let priList = viewer.scene.primitives._primitives;
        if (priList.length > 0) {
            priList.forEach((item: any) => {
                if (opacity !== 0) {
                    item.style = new Cesium.Cesium3DTileStyle({
                        color: "color('rgba(255,255,255," + opacity + ")')"
                    })
                } else {
                    item.style = new Cesium.Cesium3DTileStyle({
                        show: false
                    })

                }
            })
        }
        viewer.imageryLayers._layers.forEach((item: any) => {
            item.alpha = opacity;
        });
        return true;
    }

}

export default new MapTools();
